package member_coupon

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/util"
	"context"
)

// CheckHasReachMaxiumGotFrequencyToMemberParam 用户对券是否达到最大领取数参数
type CheckHasReachMaxiumGotFrequencyToMemberParam struct {
	CouponPtr *sqlc.Coupon
	MemberID  int64
}

// CheckHasReachMaxiumGotFrequencyToMember 用户对券是否达到最大领取数
func (s *MemberCouponEntityServiceImp) CheckHasReachMaxiumGotFrequencyToMember(ctx context.Context, q *sqlc.Queries, ptr *CheckHasReachMaxiumGotFrequencyToMemberParam) (checkRet bool, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	maxiumGotFrequency := ptr.CouponPtr.MaxiumGotFrequency
	if maxiumGotFrequency == 0 {
		return
	}
	gotCnt, aggErr := q.AggMemberCouponGotCntByMemberIDAndCouponID(ctx, sqlc.AggMemberCouponGotCntByMemberIDAndCouponIDParams{
		MemberID: ptr.MemberID,
		CouponID: ptr.CouponPtr.ID,
	})
	if aggErr != nil {
		err = aggErr
		return
	}
	slog.Infof("gotCnt:%d, maxiumGotFrequency:%d", gotCnt, maxiumGotFrequency)
	checkRet = gotCnt >= maxiumGotFrequency
	// if gotCnt >= maxiumGotFrequency {
	// 	err = fmt.Errorf("已达到最大领取数")
	// 	return
	// }

	return
}
